package com.feonix.common.view.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.feonix.common.permission.PermissionStructure;
import com.feonix.common.util.StringUtil;

public class Button extends PermissionStructure {
	private List<Button> subButtons;
	private String icon;
	private String function;
	private String value;
	private String tip;
	private boolean plain;
	private Map<String, String> domAttrs;

	public static Button getAdd() {
		return getInstance("f300a", "add", "新增");
	}

	public static Button getEdit() {
		return getInstance("f2020", "edit", "修改");
	}

	public static Button getSave() {
		return getInstance("f5021", "save", "保存");
	}

	public static Button getRemove() {
		return getInstance("f300b", "remove", "删除");
	}

	public static Button getClean() {
		return getInstance("f3001", "clean", "清空");
	}

	public static Button getImport() {
		return getInstance("f0028", "import", "导入");
	}

	public static Button getExport() {
		return getInstance("f0029", "export", "导出");
	}

	public static Button getReload() {
		return getInstance("f5022", "reload", "刷新");
	}

	public static Button getHelp() {
		return getInstance("f503a", "help", "帮助");
	}

	public static Button getBack() {
		return getInstance("f000b", "back", "返回");
	}

	public static Button getCopy() {
		return getInstance("f2017", "copy", "复制");
	}

	public static Button getPaste() {
		return getInstance("f508a", "paste", "粘贴");
	}

	public static Button getPrint() {
		return getInstance("f5003", "print", "打印");
	}

	public static Button getUndo() {
		return getInstance("f0005", "undo", "撤销");
	}

	public static Button getCut() {
		return getInstance("f201a", "cut", "剪切");
	}

	public static Button getUpload() {
		return getInstance("f002d", "upload", "上传");
	}

	public static Button getDownload() {
		return getInstance("f002c", "download", "下载");
	}

	public static Button getInstance(String icon, String function, String value) {
		return getInstance(icon, function, value, value);
	}

	public static Button getInstance(String icon, String function, String value, String tip) {
		return new Button(icon, function, value, tip);
	}

	private Button(String icon, String function, String value) {
		this(icon, function, value, value);
	}

	private Button(String icon, String function, String value, String tip) {
		this.subButtons = new ArrayList<Button>();

		this.plain = true;

		this.domAttrs = new HashMap<String, String>();

		this.icon = getIconName(icon);
		this.function = function;
		this.value = value;
		this.tip = tip;
	}

	private String getIconName(String iconName) {
		if ((StringUtil.isNotEmpty(iconName)) && (!(StringUtil.contains(iconName, "&#x")))) {
			iconName = "&#x" + iconName + ";";
		}
		return iconName;
	}

	public Button setIcon(String paramString) {
		this.icon = paramString;
		return this;
	}

	public Button setFunction(String function) {
		this.function = function;
		return this;
	}

	public Button setValue(String value) {
		this.value = value;
		return this;
	}

	public boolean isPlain() {
		return this.plain;
	}

	public String getIcon() {
		return this.icon;
	}

	public String getFunction() {
		return this.function;
	}

	public String getValue() {
		return this.value;
	}

	public String getTip() {
		return this.tip;
	}

	public Button setTip(String tip) {
		this.tip = tip;
		return this;
	}

	/**
	 * 设置权限（允许系统管理员）
	 * 
	 * @param paramString1
	 * @param paramString2
	 * @return
	 */
	public Button setPermission(String moduleId, String functionId) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		return this;
	}

	/**
	 * 设置权限（允许系统管理员）
	 * 
	 * @param moduleId
	 * @param functionId
	 * @param params
	 * @return
	 */
	public Button setPermission(String moduleId, String functionId, String params) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		setParams(params);
		return this;
	}

	/**
	 * 设置固定许可
	 * 
	 * @param paramBoolean1
	 * @param paramBoolean2
	 * @return
	 */
	public Button setAllowed(boolean paramBoolean1, boolean paramBoolean2) {
		setAllowedAdmin(Boolean.valueOf(paramBoolean1));
		setAllowedGroup(Boolean.valueOf(paramBoolean2));
		return this;
	}

	public List<Button> getSubButtons() {
		return this.subButtons;
	}

	public Button addSubButton(Button button) {
		this.subButtons.add(button);
		return this;
	}

	public Button addSubButton(Button button, boolean hasPermission) {
		if ((button != null) && (hasPermission)) {
			addSubButton(button);
		}

		return this;
	}

	public Map<String, String> getDomAttrs() {
		return this.domAttrs;
	}

	public String getDomAttrsStr() {
		String str1 = "";
		if (this.domAttrs.size() > 0) {
			Set<String> set = this.domAttrs.keySet();
			StringBuffer stringBuffer = new StringBuffer();
			for (String str2 : set) {
				stringBuffer.append(str2).append("=\"").append((String) this.domAttrs.get(str2)).append("\" ");
			}
			str1 = stringBuffer.toString().trim();
			StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		}
		return str1;
	}

	public Button addDomAttr(String paramString1, String paramString2) {
		if (StringUtil.isNotEmpty(paramString1)) {
			this.domAttrs.put(paramString1, StringUtil.getString(paramString2));
		}
		return this;
	}
}
